* ======================================
* ---------- Summary Networks ----------
* ======================================


	use "$dta\EstimationSample.dta", clear
	gen one = 1
	
	
* Panel A: Family
* ===============

	forvalues t = 2015(4)2019{
	
		preserve
		keep if year == `t'
		
		* N
		qui sum one
		local N_Fam = string(r(N), "%15.0fc")

		* Unique networks (just N for families)
		local Fam1_`t'_u = string(r(N), "%15.0fc")
		local Fam2_`t'_u = string(r(N), "%15.0fc")

		*Number of people with network = 1
		qui sum one if Fam1Any == 1
		local Fam1_`t'_n1 = string(r(N), "%15.0fc")
		qui sum one if Fam2Any == 1
		local Fam2_`t'_n1 = string(r(N), "%15.0fc")
	
		*Number of people with networkXdistrict = 1
		qui sum one if Fam1Same == 1
		local Fam1_`t'_n2 = string(r(N), "%15.0fc")
		qui sum one if Fam2Same == 1
		local Fam2_`t'_n2 = string(r(N), "%15.0fc")

		*Distance
		sum Fam1Pol_dist if Fam1Same == 1
		local Fam1_`t'_d2 = string(r(mean), "%15.2fc")
		sum Fam2Pol_dist if Fam2Same == 1
		local Fam2_`t'_d2 = string(r(mean), "%15.2fc")
		sum Fam1Pol_dist if Fam1Any == 1
		local Fam1_`t'_d = string(r(mean), "%15.2fc")
		sum Fam2Pol_dist if Fam2Any == 1
		local Fam2_`t'_d = string(r(mean), "%15.2fc")
	
		* Size of network
		rename Voter_id idPers
		merge 1:m idPers using $dta\NetworkFam2.dta, keep(matched) ///
			keepusing(Family) nogen
		rename idPers Voter_id
		sum one
		gen extended = 1*(Family >= 1 & Family <= 8) 
		gen close = 1*(Family >= 1 & Family <= 3)
		qui collapse (sum) close extended, by(Voter_id) /**/
		sum close
		local Fam1_`t'_v = string(r(mean), "%15.2fc")
		sum extended
		local Fam2_`t'_v = string(r(mean), "%15.2fc")

		restore
	}

	
* Panel B: Co-workers
* ===================

	foreach net in Work1 Work2{

	forvalues t = 2015(4)2019{
		
		preserve
		keep if year == `t'
		drop if `net'net == .
	
		* N
		qui sum `net'net
		local N_`net' = string(r(N), "%15.0fc")
	
		* Unique networks
		qui unique `net'net
		local `net'_`t'_u = string(r(unique), "%15.0fc")

		* Number of people with network = 1
		qui sum one if `net'Any == 1
		local `net'_`t'_n1 = string(r(N), "%15.0fc")
	
		* Number of people with networkXdistrict = 1
		qui sum one if `net'Same == 1
		local `net'_`t'_n2 = string(r(N), "%15.0fc")

		* Distance
		sum `net'Pol_dist if `net'Same == 1
		local `net'_`t'_d2 = string(r(mean), "%15.2fc")
		sum `net'Pol_dist if `net'Any == 1
		local `net'_`t'_d = string(r(mean), "%15.2fc")
	
		* Size of network
		qui collapse (first) Voter_id (sum) one, by(`net'net)
		sum one
		local `net'_`t'_v = string(r(mean), "%15.2fc")
		
		restore
		}
	}

	
* Panel C: Immigrants
* ===================

	foreach net in Imm1 Imm2{

		forvalues t = 2015(4)2019{
		
		preserve
		keep if year == `t'
		drop if `net'net == .
	
		* N
		qui sum `net'net
		local N_`net' = string(r(N), "%15.0fc")
	
		* Unique networks
		qui unique `net'net
		local `net'_`t'_u = string(r(unique), "%15.0fc")

		* Number of people with network = 1
		qui sum one if `net'Any == 1
		local `net'_`t'_n1 = string(r(N), "%15.0fc")
	
		* Number of people with networkXdistrict = 1
		qui sum one if `net'Same == 1
		local `net'_`t'_n2 = string(r(N), "%15.0fc")

		* Distance
		sum `net'Pol_dist if `net'Same == 1
		local `net'_`t'_d2 = string(r(mean), "%15.2fc")
		sum `net'Pol_dist if `net'Any == 1
		local `net'_`t'_d = string(r(mean), "%15.2fc")
	
		* Size of network
		qui collapse (first) Voter_id (sum) one, by(`net'net)
		sum one
		local `net'_`t'_v = string(r(mean), "%15.2fc")
		
		restore
		}
	}

	

	
* Build table
* ===========
	
	local tabhead "\begin{tabular}{lcccccc} \hline\hline"

	local panelA1  "Panel A: 2015 & \multicolumn{2}{c}{Family} & \multicolumn{2}{c}{Co-workers} &  \multicolumn{2}{c}{Immigrants} \\"
	local panelA2  " & \multicolumn{2}{c}{(N = `N_Fam')} &  \multicolumn{2}{c}{(N = `N_Work1')} &  \multicolumn{2}{c}{(N = `N_Imm1')}  \\"
	local panelA3 " & Close & Extended & Age-estbl. & Estbl. & 3-digit & 2-digit \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7}"
	local panelA4 "Number of unique networks & `Fam1_2015_u' & `Fam2_2015_u' & `Work1_2015_u' & `Work2_2015_u' & `Imm1_2015_u' & `Imm2_2015_u' \\"
	local panelA5 "Voters with $ AnyDistrict = 1$ & `Fam1_2015_n1' & `Fam2_2015_n1' & `Work1_2015_n1' & `Work2_2015_n1' & `Imm1_2015_n1' & `Imm2_2015_n1' \\"
	local panelA6 "Voters with $ SameDistrict = 1$ & `Fam1_2015_n2' & `Fam2_2015_n2' & `Work1_2015_n2' & `Work2_2015_n2' & `Imm1_2015_n2' & `Imm2_2015_n2' \\"
	local panelA7 "Network size (average) & `Fam1_2015_v' & `Fam2_2015_v' & `Work1_2015_v' & `Work2_2015_v' & `Imm1_2015_v' & `Imm2_2015_v' \\"
	local panelA8 "Distance (km) $ \vert AnyDistrict = 1$ & `Fam1_2015_d' & `Fam2_2015_d' & `Work1_2015_d' & `Work2_2015_d' & `Imm1_2015_d' & `Imm2_2015_d' \\"
	local panelA9 "Distance (km) $ \vert SameDistrict = 1$ & `Fam1_2015_d2' & `Fam2_2015_d2' & `Work1_2015_d2' & `Work2_2015_d2' & `Imm1_2015_d2' & `Imm2_2015_d2' \\"

	local panelB1  "Panel B: 2019 & \multicolumn{2}{c}{Family} &  \multicolumn{2}{c}{Co-workers} &  \multicolumn{2}{c}{Immigrants}  \\"
	local panelB2  " & \multicolumn{2}{c}{(N = `N_Fam')} &  \multicolumn{2}{c}{(N = `N_Work1')} &  \multicolumn{2}{c}{(N = `N_Imm1')}  \\"
	local panelB3 " & Close & Extended & Age-estbl. & Estbl. & 3-digit & 2-digit \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7}"
	local panelB4 "Number of unique networks & `Fam1_2019_u' & `Fam2_2019_u' & `Work1_2019_u' & `Work2_2019_u' & `Imm1_2019_u' & `Imm2_2019_u' \\"
	local panelB5 "Voters with $ AnyDistrict = 1$ & `Fam1_2019_n1' & `Fam2_2019_n1' & `Work1_2019_n1' & `Work2_2019_n1' & `Imm1_2019_n1' & `Imm2_2019_n1' \\"
	local panelB6 "Voters with $ SameDistrict = 1$ & `Fam1_2019_n2' & `Fam2_2019_n2' & `Work1_2019_n2' & `Work2_2019_n2' & `Imm1_2019_n2' & `Imm2_2019_n2' \\"
	local panelB7 "Network size (average) & `Fam1_2019_v' & `Fam2_2019_v' & `Work1_2019_v' & `Work2_2019_v' & `Imm1_2019_v' & `Imm2_2019_v' \\"
	local panelB8 "Distance (km) $ \vert AnyDistrict = 1$ & `Fam1_2019_d' & `Fam2_2019_d' & `Work1_2019_d' & `Work2_2019_d' & `Imm1_2019_d' & `Imm2_2019_d' \\"
	local panelB9 "Distance (km) $ \vert SameDistrict = 1$ & `Fam1_2019_d2' & `Fam2_2019_d2' & `Work1_2019_d2' & `Work2_2019_d2' & `Imm1_2019_d2' & `Imm2_2019_d2' \\"


	
* Write table
* ===========

	capture file close networks
	file open networks using "$tbls\SummaryNetworks.tex", write replace
	file write networks "`tabhead'" _n
	file write networks "\\" _n

	***** PANEL A *****

	file write networks "`panelA1'" _n
	file write networks "`panelA2'" _n
	file write networks "\\" _n

	file write networks "`panelA3'" _n
	file write networks "`panelA4'" _n
	file write networks "`panelA5'" _n
	file write networks "`panelA6'" _n
	file write networks "`panelA7'" _n
	file write networks "`panelA8'" _n
	file write networks "`panelA9'" _n
	file write networks "\hline \\" _n


	***** PANEL B *****

	file write networks "`panelB1'" _n
	file write networks "`panelB2'" _n
	file write networks "\\" _n

	file write networks "`panelB3'" _n
	file write networks "`panelB4'" _n
	file write networks "`panelB5'" _n
	file write networks "`panelB6'" _n
	file write networks "`panelB7'" _n
	file write networks "`panelB8'" _n
	file write networks "`panelB9'" _n
	file write networks "\hline \hline" _n

	file write networks "\end{tabular}"
	file close networks
	
	
	
/*
*  Not reported in table (reported elsewhere)
* ===========================================
	
	* Biggest immigrant groups, birthcountry (not part of table)
	preserve
	replace Voter_birthcountry = "Poland" if Voter_birthcountry == "131"
	replace Voter_birthcountry = "Pakistan" if Voter_birthcountry == "534"
	replace Voter_birthcountry = "Somalia" if Voter_birthcountry == "346"
	replace Voter_birthcountry = "Iraq" if Voter_birthcountry == "452"
	replace Voter_birthcountry = "Iran" if Voter_birthcountry == "456"
	keep if year == 2015 /*first year in sample*/
	keep if Voter_immigrant == 1
	local N = _N
	collapse (sum) one, by(Voter_birthcountry)
	gen p = one/`N'*100 /*Calc percent*/
	gsort -p
	list one p Voter_birthcountry in 1/10
	restore

	* Biggest immigrant groups (not part of table)
	preserve
	keep if year == 2015 /*first year in sample*/
	keep if Voter_immigrant == 1
	rename Voter_id idPers
	merge m:1 idPers using $dta/ImportOccupation15.dta, nogen keep(matched)
	gen occ = substr(occupation,1,3) if Voter_immigrant == 1
	
	replace occ = "Domestic, hotel and office cleaners and helpers" if occ == "911"
	replace occ = "Personal care workers in health services" if occ == "532"
	replace occ = "Shop salespersons" if occ == "522"
	replace occ = "Child care workers and teachers' aides" if occ == "531"
	replace occ = "Building frame and related trades workers" if occ == "711"

	local N = _N
	collapse (sum) one, by(occ)
	gen p = one/`N'*100
	gsort -p
	list one p occ in 1/10
	restore

	* Politicians
	use $dta/Politicians.dta, clear
	keep if year == 2015
	keep if Polit_inS == 1
	rename Polit_id idPers
	merge m:1 idPers using $dta/ImportFixed.dta, nogen keep(master matched)
	keep if immigrant == 1 
	bysort birthcountry: gen n = _N
	gen sum = _N
	gen frac = n/sum
	collapse frac, by(birthcountry)
	gsort -frac
	list birthcountry frac in 1/5

	*/
